import java.util.ArrayList;
import java.util.List;

/**
 * 工序,完成产品需要经历的工序.
 *
 * @author sk.z
 */
public class Stage {

    private int id;

    private int index;  //次序 从0开始

    private String name;

    private List<Stage> preStage = new ArrayList<>(); //进行此工序的前置工序

    private List<Machine> machineList = new ArrayList<>();  //生产设备

    //加工
    public int process(Product[] productArr) {
        int count = 0;
        for (Machine machine : machineList) {

            PRODUCT:
            for (Product product : productArr) {
                //是否符合生产条件
                if (product.getResult(this.index) > 0){
                    continue;
                }
                for (Stage pre : preStage) {
                    if (product.getResult(pre.index) == 0) {
                        continue PRODUCT;
                    }
                }
                //加工生产
                if (0 == machine.process(product, this.index)) {
                    break;
                }
                count++;
            }
        }
        return count;
    }

    public void show(){
        System.out.println("工序: index:"+index+" name:"+name);
        for (Machine m : machineList){
            System.out.println("\t"+m.toString());
        }
        System.out.println();
    }

    public Stage(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public void addPreStage(Stage stage) {
        this.preStage.add(stage);
    }

    public void addMachine(Machine machine) {
        this.machineList.add(machine);
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
